import { AudioSourceComponent } from "cc";

/**
 * 音效管理
 */
export class AudioManager {

    private constructor() { }

    /** 单例实例 */
    public static readonly instance: AudioManager = new AudioManager();

    //背景音乐管理
    private BgSound: AudioSourceComponent = null;
    //背景音乐名称
    private BgSoundName: string = "bgsound";

    //音效列表
    private SoundList: Map<string, AudioSourceComponent> = new Map();

    //是否播放音效
    public IsEffect: boolean = false;
    //是否播放背景
    public IsBg: boolean = false;

    //音量
    private Audio_Volume: number = 1;

    /**
     * 初始化
     */
    public Init() {
        this.BgSound = new AudioSourceComponent();

        this.IsBg = false;
        this.IsEffect = false;
    }

    /**
     * 播放背景音
     */
    public PlayBgSound() {
        this.BgSound.clip = app.resManager.Audio[this.BgSoundName];
        this.BgSound.play();
        this.BgSound.volume = this.Audio_Volume;
        this.BgSound.loop = true;
    }

    /**
     * 播放音效
     */
    public PlayEffect(_type: string, _audio: string) {
        if (!this.IsEffect) return;
        let _effect: AudioSourceComponent = this.SoundList.get(_type);

        if (!_effect) {
            _effect = new AudioSourceComponent();
            this.SoundList.set(_type, _effect);
        }

        _effect.clip = app.resManager.Audio[_audio];
        _effect.play();
        _effect.volume = this.Audio_Volume;
        _effect.loop = false;
    }

}


export const audioManager = AudioManager.instance;